package com.lu.statistics.fileService.impl;

import com.lu.statistics.common.configBean.SystemConfig;
import com.lu.statistics.entity.SysFile;
import com.lu.statistics.fileService.FileUploadService;
import com.lu.statistics.mapper.SysFileMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 文件上传下载公用service
 */
@Service
public class FileUploadServiceImpl implements FileUploadService {
    @Autowired
    SystemConfig systemConfig;
    @Autowired
    SysFileMapper fileMapper;


    @Override
    public SysFile uploadFile(SysFile file, InputStream in) throws IOException {
        SysFile sysFile = null;
        if (StringUtils.isNotBlank(file.getFileName())) {
            sysFile = getFilePathInfo(file.getFileNumber(), file.getFilePath(), file.getFileName(), true);
        } else {
            String fileName = file.getFileName() + file.getFileName().substring(file.getFileName().lastIndexOf("."));
            sysFile = getFilePathInfo(file.getFileNumber(), file.getFilePath(), fileName, false);
        }
        sysFile.setFileSize(file.getFileSize());
        sysFile.setFileName(file.getFileName());
        sysFile.setCreateId(file.getCreateId());
        sysFile.setFileType(file.getFileType());
        uploadFile4NFS(sysFile, in);
        if (sysFile != null) {
            fileMapper.insert(sysFile);
            return sysFile;
        }
        return null;
    }
    /**
     * 将文件保存到nfs文件服务器中
     *
     * @param file
     * @param is
     * @return
     */
    private void uploadFile4NFS(SysFile file, InputStream is) throws IOException {
        BufferedInputStream bis = null;
        FileOutputStream fos = null;
        BufferedOutputStream bos = null;
        // 判断文件目录是否存在，不存在则创建
        File filePathDir = new File(file.getFilePath());
        if (!filePathDir.isDirectory()) {
            filePathDir.mkdirs();
        }
        bis = new BufferedInputStream(is);
        fos = new FileOutputStream(file.getFileUrl());
        bos = new BufferedOutputStream(fos);
        byte[] temp = new byte[1024];
        int size = 0;
        while ((size = bis.read(temp)) != -1) {
            bos.write(temp, 0, size);
        }
        // linux文件服务器文件授权
        Runtime.getRuntime().exec("chmod 777 " + file.getFileUrl());

        if (bos != null) {
            bos.close();
        }
        if (fos != null) {
            fos.close();
        }
        if (bis != null) {
            bis.close();
        }
        if (is != null) {
            is.close();
        }
    }

    @Override
    public String getFileSavePath() {
        return systemConfig.getPhysicalPath();
    }

    @Override
    public SysFile getFilePathInfo(String fileNumber, String filePath, String fileName, boolean isFilePath) {
        SysFile file = new SysFile();
        SimpleDateFormat df = new SimpleDateFormat("yyyyMM");
        file.setFileName(fileName);
        if (StringUtils.isBlank(filePath)) {
            filePath = File.separator + fileNumber + File.separator + df.format(new Date());
        } else {
            filePath = filePath.startsWith("/") ? File.separator + fileNumber + filePath : File.separator + fileNumber + File.separator + filePath;
        }
        // 文件存储目录前缀
        String filePathPrefix = systemConfig.getPhysicalPath();
        // 文件后缀名
        String filePostfix = fileName.substring(fileName.lastIndexOf("."));
        //设置文件相对路径
        file.setFilePath(filePathPrefix + filePath);
        if (isFilePath) {
            file.setEncryptName("file" + System.currentTimeMillis());
            String fileFullPath = filePath + File.separator + file.getEncryptName() + filePostfix;
            file.setFileFullPath(fileFullPath);
            String fileUrl = filePathPrefix + filePath + File.separator + file.getEncryptName() + filePostfix;
            file.setFileUrl(fileUrl);
        } else {
            file.setEncryptName("file" + System.currentTimeMillis());
            String fileFullPath = fileName + File.separator + file.getEncryptName();
            file.setFileFullPath(fileFullPath);
            String fileUrl = filePath + File.separator + file.getEncryptName();
            file.setFileUrl(fileUrl);
        }
        file.setFileNumber(fileNumber);
        file.setFileFormat(filePostfix.substring(1, filePostfix.length()));
        return file;
    }


}
